﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MNF.Admin.Web
{
    /// <summary>
    /// added by lqm 2013.07.09 
    /// Authorize登录授权认证
    /// 还待优化 http://www.myexception.cn/vc-mfc/563920.html
    /// </summary>
    public class AuthenticationAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            return base.AuthorizeCore(httpContext);

        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            base.HandleUnauthorizedRequest(filterContext);
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);     ///执行到这里时，会调用AuthorizeCore==>>HandleUnauthorizedRequest==>>再返回
            if (string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name) && filterContext.HttpContext.Session.IsNewSession)
            {
                filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary { { "Action", "Login" }, { "Controller", "Account" }, { "returnUrl", filterContext.HttpContext.Request.Url.ToString() } });
            }

        }
    }
}